#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../../include/sort.h"


#define STRSIZE	6

static void print_idata(const int *array, int size)
{
	int idx;

	for (idx = 0; idx < size; idx++)
		fprintf(stdout, "A[%d] = %d\n", idx, array[idx]);

	return;
}

static void print_sdata(char (*array)[STRSIZE], int size)
{
	int i;

	for (i = 0; i < size; i++)
	{
		fprintf(stdout, "A[%d] = %s\n", i, array[i]);
	}

	return;
}

static int compare_int(const void *int1, const void *int2)
{
	if (*(int *)int1 > *(int *)int2)
		return 1;
	if (*(int *)int1 < *(int *)int2)
		return -1;
	else
		return 0;
}

static int compare_str(const void *str1, const void *str2)
{
	int ret;

	ret = strcmp((const char *)str1, (const char *)str2);
	if (ret > 0)
		return 1;
	if (ret < 0)
		return -1;
	else
		return 0;
}


int main(int argc, char *argv[])
{
	int iarray[10], qarray[10];
	char sarray[10][STRSIZE];
	int size = 10;

	iarray[0] = 0;
	iarray[1] = 5;
	iarray[2] = 1;
	iarray[3] = 7;
	iarray[4] = 3;
	iarray[5] = 2;
	iarray[6] = 8;
	iarray[7] = 9;
	iarray[8] = 4;
	iarray[9] = 6;

	memcpy(qarray, iarray, size * sizeof(int));

	strcpy(sarray[0], "ebcde");
	strcpy(sarray[1], "ghidj");
	strcpy(sarray[2], "ghiea");
	strcpy(sarray[3], "abaae");
	strcpy(sarray[4], "abaaa");
	strcpy(sarray[5], "abcde");
	strcpy(sarray[6], "abbcd");
	strcpy(sarray[7], "ddaab");
	strcpy(sarray[8], "faeja");
	strcpy(sarray[9], "aaaaa");

	#if 0
	fprintf(stdout, "before sorting:\n");
	print_idata(iarray, size);

	if(issort((void *)iarray, size, sizeof(int), compare_int))
		return 1;

	fprintf(stdout, "after sorting:\n");
	print_idata(iarray, size);
	#endif

	fprintf(stdout, "before sorting:\n");
	print_idata(qarray, size);

	if(qksort((void *)qarray, size, sizeof(int), 0, size - 1, compare_int))
		return 1;

	fprintf(stdout, "after sorting:\n");
	print_idata(qarray, size);

	fprintf(stdout, "before mgsort:\n");
	print_sdata(sarray, size);

	if (mgsort(sarray, size, STRSIZE, 0, size - 1, compare_str))
		return 1;

	fprintf(stdout, "after mgsort:\n");
	print_sdata(sarray, size);

	return 0;
}
